clear all
noi cls 
set matsize 1000
set maxvar 3000
drop _all
set more 1
est clear 


*log using "${code}log_fcl`: di  %tdCY-N-D  daily("$S_DATE", "DMY")'.txt",text replace 

use "${data}analysis_exit0112.dta", clear

*Only use years 2002-2012
keep if year>=2002

xtset PSID year


* Generate productivity bins using 2001 productivity ranks
tab BinPdty3_initialprod2, gen(BinPdty)

forvalues i = 1(1)3{
gen Bin`i't2mmean = BinPdty`i'*t2mmean
gen Bin`i'tmaxmean = BinPdty`i'*tmaxmean
}

forvalues i = 1(1)3{
gen Bin`i'yrpre = BinPdty`i'*yrpre
}


forvalues i = 1(1)3{
gen Bin`i'yrrh2m = BinPdty`i'*yrrh2m
}


*GENERATE DURATION DUMMIES
gen age = year - entryyear
tab age, gen(duration)

gen age2001 = 2001 - entryyear
tab age2001, gen(startage)

forvalues i = 1(1)18{

       forvalues j = 1(1)3{
    gen Bin`j'age`i' = BinPdty`j'*startage`i'

     }
	 
}

egen kabuXyear = group(kabu year)

egen prod2Xis = group(island prod2)

egen prod2Xprv = group(prv prod2)


replace exit  = exit*100

***
***
*Table 2
eststo clear

eststo exit1: quietly reghdfe exit Bin1t2mmean Bin2t2mmean Bin3t2mmean Bin1yrrh2m Bin2yrrh2m Bin3yrrh2m Bin1yrpre Bin2yrpre Bin3yrpre Bin1age1-Bin3age18, abs(yearXprod2 age kabucode) vce(cluster PSID kabuXyear)
estadd local YearIndustryFE "Yes"
estadd local KabuFE "Yes"
estadd local YearIslandFE ""
estadd local IndustryIslandFE ""
estadd local Clustering "Firm, KabuXYear"

eststo exit2: quietly reghdfe exit Bin1t2mmean Bin2t2mmean Bin3t2mmean Bin1yrrh2m Bin2yrrh2m Bin3yrrh2m Bin1yrpre Bin2yrpre Bin3yrpre Bin1age1-Bin3age18, abs(yearXis yearXprod2 age kabucode) vce(cluster PSID kabuXyear)
estadd local YearIndustryFE "Yes"
estadd local KabuFE "Yes"
estadd local YearIslandFE "Yes"
estadd local IndustryIslandFE ""
estadd local Clustering "Firm, KabuXYear"

eststo exit3: quietly reghdfe exit Bin1t2mmean Bin2t2mmean Bin3t2mmean Bin1yrrh2m Bin2yrrh2m Bin3yrrh2m Bin1yrpre Bin2yrpre Bin3yrpre Bin1age1-Bin3age18, abs(yearXis yearXprod2 prod2Xis age kabucode) vce(cluster PSID kabuXyear)
estadd local YearIndustryFE "Yes"
estadd local KabuFE "Yes"
estadd local YearIslandFE "Yes"
estadd local IndustryIslandFE "Yes"
estadd local Clustering "Firm, KabuXYear"

esttab exit1 exit2 exit3, starlevels(* 0.10 ** 0.05 *** 0.01) cells(b(star fmt(4)) se(par fmt(3))) label mlabel(exit exit exit) dropped(" ") scalar("YearIndustryFE" "YearIslandFE" "IndustryIslandFE" "Clustering" "KabuFE") noomit drop(Bin1age* Bin2age* Bin3age* Bin*yrpre Bin*yrrh2m) gaps interaction("*")

esttab exit1 exit2 exit3 using "${tables}table2.tex", starlevels(* 0.10 ** 0.05 *** 0.01) cells(b(star fmt(4)) se(par fmt(3))) label mlabel(exit exit exit) dropped(" ") scalar("YearIndustryFE" "YearIslandFE" "IndustryIslandFE" "Clustering" "KabuFE") noomit drop(Bin1age* Bin2age* Bin3age* Bin*yrpre Bin*yrrh2m) gaps interaction("*") replace

***
***
*Table A1
*Define productivity bins using control function approach
drop BinPdty1-Bin3yrrh2m Bin1age1-Bin3age18

tab BinPdty3_initialprod2_wrdg, gen(BinPdty)

forvalues i = 1(1)3{
gen Bin`i't2mmean = BinPdty`i'*t2mmean
}

forvalues i = 1(1)3{
gen Bin`i'yrpre = BinPdty`i'*yrpre
}

forvalues i = 1(1)3{
gen Bin`i'yrrh2m = BinPdty`i'*yrrh2m
}

forvalues i = 1(1)18{

       forvalues j = 1(1)3{
    gen Bin`j'age`i' = BinPdty`j'*startage`i'

     }
	 
}

eststo clear
eststo exit1: quietly reghdfe exit Bin1t2mmean Bin2t2mmean Bin3t2mmean Bin1yrrh2m Bin2yrrh2m Bin3yrrh2m Bin1yrpre Bin2yrpre Bin3yrpre Bin1age1-Bin3age18, abs(yearXprod2 age kabucode) vce(cluster PSID kabuXyear)
estadd local YearIndustryFE "Yes"
estadd local KabuFE "Yes"
estadd local YearIslandFE ""
estadd local IndustryIslandFE ""
estadd local Clustering "Firm, KabuXYear"

eststo exit2: quietly reghdfe exit Bin1t2mmean Bin2t2mmean Bin3t2mmean Bin1yrrh2m Bin2yrrh2m Bin3yrrh2m Bin1yrpre Bin2yrpre Bin3yrpre Bin1age1-Bin3age18, abs(yearXis yearXprod2 age kabucode) vce(cluster PSID kabuXyear)
estadd local YearIndustryFE "Yes"
estadd local KabuFE "Yes"
estadd local YearIslandFE "Yes"
estadd local IndustryIslandFE ""
estadd local Clustering "Firm, KabuXYear"

eststo exit3: quietly reghdfe exit Bin1t2mmean Bin2t2mmean Bin3t2mmean Bin1yrrh2m Bin2yrrh2m Bin3yrrh2m Bin1yrpre Bin2yrpre Bin3yrpre Bin1age1-Bin3age18, abs(yearXis yearXprod2 prod2Xis age kabucode) vce(cluster PSID kabuXyear)
estadd local YearIndustryFE "Yes"
estadd local KabuFE "Yes"
estadd local YearIslandFE "Yes"
estadd local IndustryIslandFE "Yes"
estadd local Clustering "Firm, KabuXYear"

esttab exit1 exit2 exit3, starlevels(* 0.10 ** 0.05 *** 0.01) cells(b(star fmt(4)) se(par fmt(3))) label mlabel(exit exit exit) dropped(" ") scalar("YearIndustryFE" "YearIslandFE" "IndustryIslandFE" "KabuFE" "Clustering") noomit drop(Bin1age* Bin2age* Bin3age* Bin*yrpre Bin*yrrh2m) gaps interaction("*") replace

esttab exit1 exit2 exit3 using "${tables}tableA1.tex", starlevels(* 0.10 ** 0.05 *** 0.01) cells(b(star fmt(4)) se(par fmt(3))) label mlabel(exit exit exit) dropped(" ") scalar("YearIndustryFE" "YearIslandFE" "IndustryIslandFE" "KabuFE" "Clustering") noomit drop(Bin1age* Bin2age* Bin3age* Bin*yrpre Bin*yrrh2m) gaps interaction("*") replace


***
***
*Table A2
eststo clear

eststo exit1: quietly reghdfe exit  Bin1t2mmean Bin2t2mmean Bin3t2mmean Bin1yrrh2m Bin2yrrh2m Bin3yrrh2m Bin1yrpre Bin2yrpre Bin3yrpre, abs(yearXprod2 kabucode) vce(cluster PSID kabuXyear)
estadd local YearIndustryFE "Yes"
estadd local YearIslandFE ""
estadd local IndustryIslandFE ""
estadd local KabuFE "Yes"
estadd local Clustering "Firm, KabuXYear"


eststo exit2: quietly reghdfe exit Bin1t2mmean Bin2t2mmean Bin3t2mmean Bin1yrrh2m Bin2yrrh2m Bin3yrrh2m Bin1yrpre Bin2yrpre Bin3yrpre, abs(yearXprod2 prod2Xis kabucode) vce(cluster PSID kabuXyear)
estadd local YearIndustryFE "Yes"
estadd local YearIslandFE ""
estadd local IndustryIslandFE "Yes"
estadd local KabuFE "Yes"
estadd local Clustering "Firm, KabuXYear"

eststo exit3: quietly reghdfe exit Bin1t2mmean Bin2t2mmean Bin3t2mmean Bin1yrrh2m Bin2yrrh2m Bin3yrrh2m Bin1yrpre Bin2yrpre Bin3yrpre, abs(yearXis yearXprod2 prod2Xis kabucode) vce(cluster PSID kabuXyear)
estadd local YearIndustryFE "Yes"
estadd local YearIslandFE "Yes"
estadd local IndustryIslandFE "Yes"
estadd local KabuFE "Yes"
estadd local Clustering "Firm, KabuXYear"


esttab exit1 exit2 exit3, starlevels(* 0.10 ** 0.05 *** 0.01) cells(b(star fmt(4)) se(par fmt(3))) label mlabel(exit exit exit) dropped(" ") scalar("YearIndustryFE" "YearIslandFE" "IndustryIslandFE" "KabuFE" "Clustering") noomit drop( Bin*yrpre Bin*yrrh2m) gaps interaction("*") replace

esttab exit1 exit2 exit3 using "${tables}tableA1.tex", starlevels(* 0.10 ** 0.05 *** 0.01) cells(b(star fmt(4)) se(par fmt(3))) label mlabel(exit exit exit) dropped(" ") scalar("YearIndustryFE" "YearIslandFE" "IndustryIslandFE" "KabuFE" "Clustering") noomit drop(Bin*yrpre Bin*yrrh2m) gaps interaction("*") replace


***
***
*Table A3
eststo clear
									
eststo exit1: quietly reghdfe exit Bin1t2mmean Bin2t2mmean Bin3t2mmean Bin1yrrh2m Bin2yrrh2m Bin3yrrh2m Bin1yrpre Bin2yrpre Bin3yrpre Bin1age1-Bin3age18 if num_workers>40, abs(c.year##island c.year##prod2 prod2Xis age kabucode) vce(cluster PSID kabuXyear)
estadd local YearIndustryTrends "Yes"
estadd local YearIslandTrends "Yes"
estadd local IndustryIslandFE "Yes"
estadd local KabuFE "Yes"
estadd local Clustering "Firm, KabuXYear"
			
eststo exit2: quietly reghdfe exit Bin1t2mmean Bin2t2mmean Bin3t2mmean Bin1yrrh2m Bin2yrrh2m Bin3yrrh2m Bin1yrpre Bin2yrpre Bin3yrpre Bin1age1-Bin3age18 if num_workers>50, abs(c.year##island c.year##prod2 prod2Xis age kabucode) vce(cluster PSID kabuXyear)
estadd local YearIndustryTrends "Yes"
estadd local YearIslandTrends "Yes"
estadd local IndustryIslandFE "Yes"
estadd local KabuFE "Yes"
estadd local Clustering "Firm, KabuXYear"

eststo exit3: quietly reghdfe exit Bin1t2mmean Bin2t2mmean Bin3t2mmean Bin1yrrh2m Bin2yrrh2m Bin3yrrh2m Bin1yrpre Bin2yrpre Bin3yrpre Bin1age1-Bin3age18 if num_workers>60, abs(c.year##island c.year##prod2 prod2Xis age kabucode) vce(cluster PSID kabuXyear)
estadd local YearIndustryTrends "Yes"
estadd local YearIslandTrends "Yes"
estadd local IndustryIslandFE "Yes"
estadd local KabuFE "Yes"
estadd local Clustering "Firm, KabuXYear"
			
eststo exit4: quietly reghdfe exit Bin1t2mmean Bin2t2mmean Bin3t2mmean Bin1yrrh2m Bin2yrrh2m Bin3yrrh2m Bin1yrpre Bin2yrpre Bin3yrpre Bin1age1-Bin3age18 if num_workers>40, abs(yearXis yearXprod2 prod2Xis age kabucode) vce(cluster PSID kabuXyear)
estadd local YearIndustryFE "Yes"
estadd local KabuFE "Yes"
estadd local YearIslandFE "Yes"
estadd local IndustryIslandFE "Yes"
estadd local Clustering "Firm, KabuXYear"

eststo exit5: quietly reghdfe exit Bin1t2mmean Bin2t2mmean Bin3t2mmean Bin1yrrh2m Bin2yrrh2m Bin3yrrh2m Bin1yrpre Bin2yrpre Bin3yrpre Bin1age1-Bin3age18 if num_workers>50, abs(yearXis yearXprod2 prod2Xis age kabucode) vce(cluster PSID kabuXyear)
estadd local YearIndustryFE "Yes"
estadd local KabuFE "Yes"
estadd local YearIslandFE "Yes"
estadd local IndustryIslandFE "Yes"
estadd local Clustering "Firm, KabuXYear"

eststo exit6: quietly reghdfe exit Bin1t2mmean Bin2t2mmean Bin3t2mmean Bin1yrrh2m Bin2yrrh2m Bin3yrrh2m Bin1yrpre Bin2yrpre Bin3yrpre Bin1age1-Bin3age18 if num_workers>60, abs(yearXis yearXprod2 prod2Xis age kabucode) vce(cluster PSID kabuXyear)
estadd local YearIndustryFE "Yes"
estadd local KabuFE "Yes"
estadd local YearIslandFE "Yes"
estadd local IndustryIslandFE "Yes"
estadd local Clustering "Firm, KabuXYear"

esttab exit1 exit2 exit3 exit4 exit5 exit6, starlevels(* 0.10 ** 0.05 *** 0.01) cells(b(star fmt(4)) se(par fmt(3))) label mlabel(exit exit exit) dropped(" ") scalar("YearIndustryTrends" "YearIslandTrends" "KabuFE" "YearIndustryFE" "YearIslandFE" "IndustryIslandFE" "Clustering") noomit drop(Bin1age* Bin2age* Bin3age* Bin*yrpre Bin*yrrh2m) gaps interaction("*") 

esttab exit1 exit2 exit3 exit4 exit5 exit6 using "${tables}tableA3.tex", starlevels(* 0.10 ** 0.05 *** 0.01) cells(b(star fmt(4)) se(par fmt(3))) label mlabel(exit exit exit) dropped(" ") scalar("YearIndustryTrends" "YearIslandTrends" "KabuFE" "YearIndustryFE" "YearIslandFE" "IndustryIslandFE" "Clustering") noomit drop(Bin1age* Bin2age* Bin3age* Bin*yrpre Bin*yrrh2m) gaps interaction("*") 


***
***
*Table A4
gen LeadYear = year+1

sort kabucode LeadYear

merge m:1 kabucode LeadYear using "${masterweather}F_temp_yearly.dta"

keep if _m==3

drop _m

forvalues i = 1(1)3{
gen Bin`i'Ft2mmean = BinPdty`i'*F_t2mmean
}

forvalues i = 1(1)3{
gen Bin`i'Fyrpre = BinPdty`i'*F_yrpre
}

forvalues i = 1(1)3{
gen Bin`i'Fyrrh2m = BinPdty`i'*F_yrrh2m
}

eststo clear

eststo exit1: quietly reghdfe exit  Bin1t2mmean Bin2t2mmean Bin3t2mmean Bin1Ft2mmean Bin2Ft2mmean Bin3Ft2mmean Bin1yrrh2m Bin2yrrh2m Bin3yrrh2m Bin1yrpre Bin2yrpre Bin3yrpre Bin1age1-Bin3age18, abs(yearXprod2 age kabucode) vce(cluster PSID kabuXyear)
estadd local YearIndustryFE "Yes"
estadd local KabuFE "Yes"
estadd local YearIslandFE ""
estadd local IndustryIslandFE ""
estadd local Clustering "Firm, KabuXYear"

eststo exit2: quietly reghdfe exit Bin1t2mmean Bin2t2mmean Bin3t2mmean Bin1Ft2mmean Bin2Ft2mmean Bin3Ft2mmean Bin1yrrh2m Bin2yrrh2m Bin3yrrh2m Bin1yrpre Bin2yrpre Bin3yrpre Bin1age1-Bin3age18, abs(yearXprod2 yearXis age kabucode) vce(cluster PSID kabuXyear)
estadd local YearIndustryFE "Yes"
estadd local KabuFE "Yes"
estadd local YearIslandFE "Yes"
estadd local IndustryIslandFE ""
estadd local Clustering "Firm, KabuXYear"

eststo exit3: quietly reghdfe exit Bin1t2mmean Bin2t2mmean Bin3t2mmean Bin1Ft2mmean Bin2Ft2mmean Bin3Ft2mmean Bin1yrrh2m Bin2yrrh2m Bin3yrrh2m Bin1yrpre Bin2yrpre Bin3yrpre Bin1age1-Bin3age18, abs(yearXis yearXprod2 prod2Xis age kabucode) vce(cluster PSID kabuXyear)
estadd local YearIndustryFE "Yes"
estadd local KabuFE "Yes"
estadd local YearIslandFE "Yes"
estadd local IndustryIslandFE "Yes"
estadd local Clustering "Firm, KabuXYear"


esttab exit1 exit2 exit3, starlevels(* 0.10 ** 0.05 *** 0.01) cells(b(star fmt(4)) se(par fmt(3))) label mlabel(exit exit exit) dropped(" ") scalar("YearIndustryFE" "YearIslandFE" "IndustryIslandFE" "KabuFE" "Clustering") noomit drop(Bin1age* Bin2age* Bin3age* Bin*yrpre Bin*yrrh2m) gaps interaction("*") replace

esttab exit1 exit2 exit3 using "${tables}tableA4.tex", starlevels(* 0.10 ** 0.05 *** 0.01) cells(b(star fmt(4)) se(par fmt(3))) label mlabel(exit exit exit) dropped(" ") scalar("YearIndustryFE" "YearIslandFE" "IndustryIslandFE" "KabuFE" "Clustering") noomit drop(Bin1age* Bin2age* Bin3age* Bin*yrpre Bin*yrrh2m) gaps interaction("*") replace


























































